package com.cx330;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

public class Example68 {
    @Test
    public void test1() {
        ArrayList<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        // 使用普通for循环遍历
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            System.out.println(s);
        }
    }

    @Test
    public void test2() {
        ArrayList<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        // 使用增强for循环遍历
        for (String s : list) {
            System.out.println(s);
        }
    }

    @Test
    public void test3() {
        ArrayList<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        // 使用Iterator迭代器遍历
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String s = iterator.next();
            System.out.println(s);
        }
    }

    @Test
    public void test4() {
        ArrayList<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        // 使用ListIterator迭代器遍历
        ListIterator<String> iterator = list.listIterator();
        System.out.println("从前往后遍历：");
        while (iterator.hasNext()) {
            String s = iterator.next();
            System.out.println(s);
        }
        System.out.println("从后往前遍历：");
        while (iterator.hasPrevious()) {
            String s = iterator.previous();
            System.out.println(s);
        }
    }
}
